const util = require('../../../utils/util.js')
const domainName = getApp().globalData.domainName
const oss_domain = getApp().globalData.oss_domain
const parameter = getApp().globalData.parameter

const user_id = wx.getStorageSync('user_id')
const jg_stu_id = wx.getStorageSync('jg_stu_id')
 
const windowHeight = wx.getSystemInfoSync().windowHeight

Page({

  /**
   * 页面的初始数据
   */
  data: { 
    tis:[{ti_id:null}],
    windowHeight:windowHeight,
    icon_url:oss_domain + 'static/picture/app_icon/',
    current_list_index:0,
    list_show:[],
    ti_bookmarks:[],
  },
  // 开关抽屉
  open_drawer(){this.setData({view_card:true})},
  close_drawer(){this.setData({view_card:false})},




  // 滑动swiper事件
  swiperchange(e){
    // console.log('滑动swiper事件',e)
    let source = e.detail.source
    var current = e.detail.current
    var list_show = this.data.list_show
    if(source=="touch"){
      if(this.admin==1){
        this.swiperchange_logic(current,list_show)
      }else if(this.disable!=1){
        this.swiperchange_logic(current,list_show)
      }else{
        getApp().apply_get_right()
      }
    }
  },

  swiperchange_logic(current,list_show){
    if(this.data.last_current_index==0 && current==1){
      // console.log('0->1')
      var current_dic = this.ti_list[this.data.current_list_index+2]
      if(current_dic){
        list_show.splice(2,1,current_dic)   // 将索引为2的滑块内容换为题 current_dic
        this.setData({
          current_list_index:this.data.current_list_index + 1,
          list_show:list_show,
          last_current_index:current,
        })
      }else{
        this.go_ti_now(this.ti_list.length-1)
      }
    }else if(this.data.last_current_index==1 && current==2){
      // console.log('1->2')  // 正常的，不用处理
      var current_dic = this.ti_list[this.data.current_list_index+2]
      if(current_dic){
        list_show.splice(0,1,current_dic)  // 将索引为0的滑块内容换为题 current_dic
        this.setData({
          current_list_index:this.data.current_list_index + 1,
          list_show:list_show,
          last_current_index:current,
        })
      }else{
        this.go_ti_now(this.ti_list.length-1)
      }
    }else if(this.data.last_current_index==2 && current == 0){
      // console.log('2->0')
      var current_dic = this.ti_list[this.data.current_list_index+2]
      if(current_dic){
        list_show.splice(1,1,current_dic)  // 将索引为1的滑块内容换为题 current_dic
        this.setData({
          current_list_index:this.data.current_list_index + 1,
          list_show:list_show,
          last_current_index:current,
        })
      }else{
        this.go_ti_now(this.ti_list.length-1)
      }
      /***
       * 以下为后退**/
    }else if(this.data.last_current_index==1 && current == 0){
      // console.log('1->0')
      var current_dic = this.ti_list[this.data.current_list_index-2]
      if(current_dic){
        list_show.splice(2,1,current_dic) // 将索引为2的滑块内容换为题 current_dic
        this.setData({
          list_show:list_show,
          last_current_index:current,
          current_list_index:this.data.current_list_index - 1,
        })
      }else{
        this.go_ti_now(0)
      }
    }else if(this.data.last_current_index==0 && current == 2){
      // console.log('0->2')
      var current_dic = this.ti_list[this.data.current_list_index-2]
      if(current_dic){
        list_show.splice(1,1,current_dic)  // 将索引为1的滑块内容换为题 current_dic
        this.setData({
          list_show:list_show,
          last_current_index:current,
          current_list_index:this.data.current_list_index - 1,
        })
      }else{
        this.go_ti_now(0)
      }
    }else if(this.data.last_current_index==2 && current == 1){
      // console.log('2->1')
      var current_dic = this.ti_list[this.data.current_list_index-2]
      if(current_dic){
        list_show.splice(0,1,current_dic)  // 将索引为0的滑块内容换为题 current_dic
        this.setData({
          current_list_index:this.data.current_list_index - 1,
          list_show:list_show,
          last_current_index:current,
        })
      }else{
        this.go_ti_now(0)
      }
    }
    this.start_read_time = util.formatTime(new Date())
  },

    
  // 前往习题
  go_ti_now(index){
    if(index==0){
      this.setData({
        current_index:0,
        list_show:this.ti_list.slice(index,index+2).concat({}),
        last_current_index:0, // 默认最近current==0
      })
    }else if(index==this.ti_list.length-1){
      this.setData({
        current_index:2,
        list_show:[{}].concat(this.ti_list.slice(index-1,this.ti_list.length)),
        last_current_index:2, // 默认最近current==2
      })
    }else{
      this.setData({
        current_index:1,
        list_show:this.ti_list.slice(index-1,index+2),
        last_current_index:1, // 默认最近current==1
      })
    }
    this.start_read_time = util.formatTime(new Date())
    this.setData({
      current_list_index:index,
    })
    
    // 关闭抽屉
    this.close_drawer()
  },

  // 前往习题
  go_ti(e){

    if(this.admin!=1 && this.disable==1){
      getApp().apply_get_right()
    }
    let index = parseInt(e.currentTarget.id)
    this.go_ti_now(index)
  },



  // 收藏题
  set_bookmark(e){
    if(!jg_stu_id){
      wx.showModal({
        content: '在训学员方可收藏习题！',
        title: '提示',
      })
      return
    }
    let ti_id = this.ti_list[this.data.current_list_index].ti_id
    wx.request({
      url: domainName + '/add_bookmarkTi',
      data: {
        ti_id:ti_id,
        jg_stu_id:jg_stu_id,

      },...parameter,
      success: res => {
        let ti_bookmarks = res.data.lis
        this.setData({
          ti_bookmarks:ti_bookmarks
        })
        var pages = getCurrentPages()
        var pre_page = pages[pages.length-2]
        pre_page.setData({
          mark_count:ti_bookmarks.length
        })
      },
    })
  },





  // 重新加载习题列表
  load_ti_list(tis){
    // 加载新习题列表
    let current_list_index = 0
    var list_show = tis.slice(0,3)
    this.setData({
      last_current_index:current_list_index,
      current_list_index:current_list_index,
      list_show:list_show,
    })
    this.start_read_time = util.formatTime(new Date())
    this.ti_list = tis
  },

  // 判断用户做题权限
  page_init(user_id){
    wx.request({
      url: domainName + '/read_tis_init',
      data: {
        user_id:user_id,
      },...parameter,
      success: res => {
        console.log('res',res.data)
        this.admin = res.data.admin
        this.setData({
          ti_bookmarks:res.data.lis,
        })
      },
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('options',options)
    this.page_init(user_id, jg_stu_id)
    if(options.page_title){
      wx.setNavigationBarTitle({
        title: options.page_title,
      })
    }

    let tis = JSON.parse(decodeURIComponent(options.tis))  // 要做的习题列表,
    this.setData({
      tis:tis  // 要做的习题列表,
    })

    // 本地配置习题
    this.load_ti_list(tis)
    this.disable = options.disable
    console.log('this.disable',this.disable, typeof this.disable)
  },


   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    getApp().get_vibration()
    return {
      path: '/pages/doTi/show_ti/show_ti?ti_id=' + this.ti_list[this.data.current_list_index].ti_id
    }
  },
})